package com.tanhua.manager.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.tanhua.domain.db.Log;
import com.tanhua.manager.api.db.LogApi;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhua-log", consumerGroup = "tanhua-log-consumer")
public class LogMessageListener implements RocketMQListener<Map> {

    @Reference
    private LogApi logApi;

    @Override
    public void onMessage(Map message) { // 注意不要指定泛型，mq会报错
        System.out.println(message);
        // 封装log对象
        String userId = (String) message.get("userId");
        String type = (String) message.get("type");
        String logTime = (String) message.get("logTime");
        String place = (String) message.get("place");
        String equipment = (String) message.get("equipment");
        Log log = new Log();
        log.setUserId(Long.valueOf(userId));
        log.setType(type);
        log.setLogTime(logTime);
        log.setPlace(place);
        log.setEquipment(equipment);
        // 调用LogApi保存
        logApi.save(log);
    }
}
